<template>
    <div class="oauth-authorize">
        <h3 style="text-align: center">
            {{clientInfo.client_id}} 应用请求授权<br>
            该应用将获取你的以下信息
            <p>账号: {{userInfo.account}}</p>
            <p>昵称: {{userInfo.name}}</p>
        </h3>
        <el-form v-model="authFrom" id="authForm" action="http://localhost:8901/api/oauth/authorize" method="post">
            <input id="user_oauth_approval" name="user_oauth_approval" value="false" type="hidden"></input>
            <input id="scope" name="scope.all" value='false' type="hidden"></input>
            <el-form-item style="text-align: center">
                <el-button type="primary" @click="handleSubmit">授权</el-button>
                <el-button @click="handleCancel">取消</el-button>
            </el-form-item>
        </el-form>

    </div>
</template>

<script>
    import router from '../../router'
    import {getAuthorize, getLoginUser} from "../../api/manage/login";
    import AuthorizationUtils from "../../utils/AuthorizationUtils";

    export default {
        name: "Authorization",
        data() {
            return {
                userInfo: {
                    account: "",
                    name: ""
                },
                clientInfo: {
                    client_id: "",
                    response_type: "",
                    scope: "",
                    redirect_uri: "",
                },
                authFrom: {
                    user_oauth_approval: "false",
                    scope: "false"
                }
            }
        },
        methods: {
            handleSubmit() {
                document.getElementById('user_oauth_approval').value = true;
                document.getElementById('scope').value = true;
                document.getElementById('authForm').submit();
            },
            handleCancel() {
                document.getElementById('authForm').submit();
            },
            getLoginUser() {
                getLoginUser().then(response => {
                    if (response.code == 200) {
                        this.userInfo.account = response.data.user.account
                        this.userInfo.name = response.data.user.name
                    }else {
                        sessionStorage.setItem("clientInfo", JSON.stringify(this.clientInfo))
                        router.push({path: '/login', query: { isAuthorization: "authorization" }})
                    }
                    console.log(response.data)
                }).catch(error => {
                    console.log(error)
                })
            },
            getAuthorize() {
                getAuthorize(this.clientInfo).then(response => {
                    console.log(response.data)
                }).catch(error => {
                    console.log(error)
                })
            }
        },
        created() {
            this.clientInfo.client_id = this.$route.query.client_id
            this.clientInfo.response_type = this.$route.query.response_type
            this.clientInfo.scope = this.$route.query.scope
            this.clientInfo.redirect_uri = this.$route.query.redirect_uri
            this.getLoginUser()
            this.getAuthorize()
        }
    }
</script>

<style scoped>
    .oauth-authorize {
        margin: 0 auto;
        width: 300px;
    }
</style>